package com.market.plus.core.dao.jpa;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.market.plus.core.dao.ParticipantDao;
import com.market.plus.core.domain.Album;
import com.market.plus.core.domain.AlbumType;
import com.market.plus.core.domain.LockStatus;
import com.market.plus.core.domain.Participant;
import com.market.plus.core.domain.Relationship;
import com.market.plus.core.domain.UserInfo;

@Repository("participantDao")
public class JpaParticipantDao extends JpaGenericDao<Participant> implements ParticipantDao {

   public JpaParticipantDao() {
      super(Participant.class);
   }

   @SuppressWarnings("unchecked")
   public List<Participant> findAll() {
      List<Participant> participants = this.findByNamedQuery(Participant.FIND_ALL);
      return participants;
   }

   @SuppressWarnings("unchecked")
   public UserInfo findByUsername(String username) {
      Map<String, Object> params = new HashMap<String, Object>();
      params.put(UserInfo.PARA_USERNAME, username);
      List<UserInfo> userInfos = this.findByNamedQueryAndNamedParams(UserInfo.FIND_BY_USERNAME, params);
      if (userInfos.size() > 0) {
         return userInfos.get(0);
      }
      return null;
   }

   @SuppressWarnings("unchecked")
   public Participant findParticipantByUsername(String username) {
      Map<String, Object> params = new HashMap<String, Object>();
      params.put(Participant.PARA_USERNAME, username);
      List<Participant> participants = this.findByNamedQueryAndNamedParams(Participant.FIND_USERNAME, params);
      if (participants.size() > 0) {
         return participants.get(0);
      }
      return null;
   }

   @SuppressWarnings("unchecked")
   public Participant findParticipantByGUID(String guid) {
      Map<String, Object> params = new HashMap<String, Object>();
      params.put(Participant.PARA_GUID, guid);
      List<Participant> participants = this.findByNamedQueryAndNamedParams(Participant.FIND_GUID, params);
      if (participants.size() > 0) {
         return participants.get(0);
      }
      return null;
   }

   @SuppressWarnings("unchecked")
   public Relationship findByRelationship(long participantId, String relationName) {
      Map<String, Object> params = new HashMap<String, Object>();
      params.put(Relationship.PARA_ID, participantId);
      params.put(Relationship.PARA_NAME, relationName);
      List<Relationship> relationships = this.findByNamedQueryAndNamedParams(Relationship.FIND_BY_NAME, params);
      if (relationships.size() > 0) {
         return relationships.get(0);
      }
      return null;
   }

   @SuppressWarnings("unchecked")
   public Album findByAlbumName(long participantId, String albumName) {
      Map<String, Object> params = new HashMap<String, Object>();
      params.put(Album.PARA_PARTICIPANTID, participantId);
      params.put(Album.PARA_NAME, albumName);
      List<Album> albums = this.findByNamedQueryAndNamedParams(Album.FIND_BY_NAME, params);
      if (albums.size() > 0) {
         return albums.get(0);
      }
      return null;
   }

   @SuppressWarnings("unchecked")
   public Album findByAlbumType(long participantId, AlbumType albumType) {
      Map<String, Object> params = new HashMap<String, Object>();
      params.put(Album.PARA_PARTICIPANTID, participantId);
      params.put(Album.PARA_TYPE, albumType);
      List<Album> albums = this.findByNamedQueryAndNamedParams(Album.FIND_BY_TYPE, params);
      if (albums.size() > 0) {
         return albums.get(0);
      }
      return null;
   }

   @SuppressWarnings("unchecked")
   public List<Participant> findWaitingFriends(long participantId) {
      Map<String, Object> params = new HashMap<String, Object>();
      params.put(Participant.PARA_ID, participantId);
      params.put(Participant.PARA_LOCKSTATUS, LockStatus.REQUESTED);
      List<Participant> participants = this.findByNamedQueryAndNamedParams(Participant.FIND_WAITINGFRIENDS, params);
      return participants;
   }

   @SuppressWarnings("unchecked")
   public List<Participant> findMyFriends(long participantId) {
      Map<String, Object> params = new HashMap<String, Object>();
      params.put(Participant.PARA_ID, participantId);
      params.put(Participant.PARA_LOCKSTATUS1, LockStatus.AVAIABLE.getFriendStatus());
      params.put(Participant.PARA_LOCKSTATUS2, LockStatus.AVAIABLE.getFriendStatus());
      List<Participant> participants = this.findByNativeQueryAndNamedParams(Participant.FIND_SQL_FRIENDS, params);
      return participants;
   }

   @SuppressWarnings("unchecked")
   public List<Participant> findMyFriends(long participantId, List<Long> excludeIds) {
      Map<String, Object> params = new HashMap<String, Object>();
      params.put(Participant.PARA_ID, participantId);
      params.put(Participant.PARA_LOCKSTATUS1, LockStatus.AVAIABLE.getFriendStatus());
      params.put(Participant.PARA_LOCKSTATUS2, LockStatus.AVAIABLE.getFriendStatus());
      params.put(Participant.PARA_IDs, excludeIds);
      List<Participant> participants = this.findByNativeQueryAndNamedParams(Participant.FIND_SQL_EXT_FRIENDS, params);
      return participants;
   }

   @SuppressWarnings("unchecked")
   public List<Participant> findNotMyFriends(long participantId) {
      Map<String, Object> params = new HashMap<String, Object>();
      params.put(Participant.PARA_ID, participantId);
      List<Participant> participants = this.findByNativeQueryAndNamedParams(Participant.FIND_SQL_NOTFRIENDS, params);
      return participants;
   }

   @SuppressWarnings("unchecked")
   public List<Participant> findMyLockedFriends(long participantId) {
      Map<String, Object> params = new HashMap<String, Object>();
      params.put(Participant.PARA_ID, participantId);
      params.put(Participant.PARA_LOCKSTATUS1, LockStatus.ISLOCKED.getFriendStatus());
      params.put(Participant.PARA_LOCKSTATUS2, LockStatus.LOCKED.getFriendStatus());
      List<Participant> participants = this.findByNativeQueryAndNamedParams(Participant.FIND_SQL_FRIENDS, params);
      return participants;
   }
}
